/**
 * Created by evantang on 2018/4/13.
 */
import {Component, ElementRef, EventEmitter, forwardRef, Input, Output, ViewChild} from "@angular/core";
import {BaseAuthenticateComponent} from "../baseAuthenticate.component";
import {WeUIGalleryComponent} from "../imageUpLoader/weUIGalleryComponent";
import {WeUIFile} from "angular4-weui";
import {ActivatedRoute, Router} from "@angular/router";
import {DialogService} from "../../../service/dialogService";
import {DomSanitizer} from "@angular/platform-browser";
import {FileService} from "../../../service/fileService";
@Component({
  selector: 'app-image-viewer',
  templateUrl: 'imageViewer.component.html',
  styleUrls: ['imageViewer.component.css']
})
export class ImageViewerComponent extends BaseAuthenticateComponent {

  /**
   * 图片预览控件
   */
  @ViewChild(forwardRef(() => WeUIGalleryComponent)) gallery: WeUIGalleryComponent;

  /**
   * 图片大小提示控件
   */
    // @ViewChild('remind') remindSImage: WeUIDialog;

  /**
   * 预览时用
   * @internal
   */
  public image: WeUIFile;

  @Input() files: WeUIFile[] = [];
  @Output() fileChange = new EventEmitter<WeUIFile>();


  constructor(protected a_router: Router, public elementRef: ElementRef, public _dialogService: DialogService,
              private sanitizer: DomSanitizer, public route: ActivatedRoute, private _fileService: FileService) {
    super(a_router, elementRef, route, false);
  }

  /**
   * 获取列表中的文件
   */
  getFiles(): WeUIFile[] {
    return this.files;
  }

  /**
   * 文件总数
   * @internal
   */
  getFileCount(): number {
    return this.files.length;
  }

  /**
   * 已上传文件数
   * @internal
   */
  getUploadedFileCount(): number {
    return this.files.filter((file: WeUIFile) => file.isUploaded).length;
  }


  /**
   * 预览图片
   */
  preview(file: WeUIFile): void {
    this.image = file;
    if (this.gallery) {
      this.gallery.show();
    }
  }

  /**
   * 退出预览
   */
  fadeOut(): void {
    if (this.gallery) {
      this.gallery.hide();
    }
  }

  /**
   * 删除文件
   * @param {file} 需要从列表中删除的图片
   */
  onDelete(file: WeUIFile): void {
    const index = this.files.indexOf(file);
    if (index >= 0) {
      this.files.splice(index, 1);
    }
  }

}
